package com.houdoudou.bean;

import javax.servlet.http.HttpServletRequest;

public class Owner {

	String telnum="";
	String serviceTelnum="";
	String name="";
	String address="";
	String secret="";
	int isAuth=0;
	double rate=0d; //公司和业主商定的提成比例，该比例属于公司
		
	public Owner() {
	
	}	
	public String getTelnum() {
		return telnum;
	}
	public void setTelnum(String telnum) {
		this.telnum = telnum;
	}
	public String getServiceTelnum() {
		return serviceTelnum;
	}
	public void setServiceTelnum(String serviceTelnum) {
		this.serviceTelnum = serviceTelnum;
	}
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public String getAddress() {
		return address;
	}
	public void setAddress(String address) {
		this.address = address;
	}
	public String getSecret() {
		return secret;
	}
	public void setSecret(String secret) {
		this.secret = secret;
	}
	public int getIsAuth() {
		return isAuth;
	}
	public void setIsAuth(int isAuth) {
		this.isAuth = isAuth;
	}
	public double getRate() {
		return rate;
	}
	public void setRate(double rate) {
		this.rate = rate;
	}

	

	public static Owner beanFromRequest(HttpServletRequest req)  {
		
	
		
		
		
		Owner owner= new Owner();
		
		if(req.getParameter("telnum")!=null){	
				owner.setTelnum(req.getParameter("telnum"));
	           }
		if(req.getParameter("serviceTelnum")!=null){	
			owner.setServiceTelnum(req.getParameter("serviceTelnum"));
           }
		if(req.getParameter("name")!=null){	
			owner.setName(req.getParameter("name"));
           }
		if(req.getParameter("address")!=null){	
			owner.setAddress(req.getParameter("address"));
           }
		if(req.getParameter("secret")!=null){	
			owner.setSecret(req.getParameter("secret"));
           }
		if(req.getParameter("isAuth")!=null){	
			if(!req.getParameter("isAuth").isEmpty()){
			int isAuth=Integer.parseInt(req.getParameter("isAuth"));
			owner.setIsAuth(isAuth);}
           }
		if(req.getParameter("rate")!=null){	
			if(!req.getParameter("rate").isEmpty()){
			double rate=Double.parseDouble(req.getParameter("rate"));
			owner.setRate(rate);}
           }
		
		System.out.println("OwnerBean  "+"   telnum="+owner.getTelnum()+" serviceTelnum="+owner.getServiceTelnum()
				+"   name="+owner.getName()+"  address="+owner.getAddress()+"secret="+owner.getSecret()
				+"  isAuth="+owner.getIsAuth()+"   rate="+owner.getRate());
		return owner;
	}

	public static  Owner merge(Owner ownerExist, Owner ownerGiven){
		
		
		if(ownerGiven.getTelnum()!=null){	
			if(!ownerGiven.getTelnum().isEmpty()){
				ownerExist.setTelnum(ownerGiven.getTelnum());
			}
		}		

		if(ownerGiven.getServiceTelnum()!=null){	
			if(!ownerGiven.getServiceTelnum().isEmpty()){
				ownerExist.setServiceTelnum(ownerGiven.getServiceTelnum());
			}
		}		

		if(ownerGiven.getName()!=null){	
			if(!ownerGiven.getName().isEmpty()){
				ownerExist.setName(ownerGiven.getName());
			}
		}		

		if(ownerGiven.getAddress()!=null){	
			if(!ownerGiven.getAddress().isEmpty()){
				ownerExist.setAddress(ownerGiven.getAddress());
			}
		}		

		if(ownerGiven.getSecret()!=null){	
			if(!ownerGiven.getSecret().isEmpty()){
				ownerExist.setSecret(ownerGiven.getSecret());
			}
		}		

		if(ownerGiven.getIsAuth()!=0){			
				ownerExist.setIsAuth(ownerGiven.getIsAuth());
		}		

		if(ownerGiven.getRate()>=0&&ownerGiven.getRate()<1){				
				ownerExist.setRate(ownerGiven.getRate());			
		}		

		
		return ownerExist;
	}		
	public static void checkValid(Owner owner) throws Exception{
		if(owner.getTelnum().length()!=11){{ throw new BeanParseException("电话位数不对"); }	}
		if(owner.getSecret().length()==0){{ throw new BeanParseException("密码为空"); }	}
	}
	
	public static Owner encrypt(Owner owner) throws Exception{
	     String cipertext= PasswordUtil.encrypt(owner.getSecret());
		owner.setSecret(cipertext);
		return owner;
	}
	public static Owner decrypt(Owner owner) throws Exception{
	    String plaintext= PasswordUtil.decrypt(owner.getSecret());
		owner.setSecret(plaintext);
		return owner;
	}
}
